package com.memorypalace.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.memorypalace.deck.biz.INoteTypeBiz;
import com.memorypalace.deck.biz.NoteTypeBiz;
import com.memorypalace.deck.mapper.NoteTypeMapper;
import com.memorypalace.pojo.User;
import com.memorypalace.user.biz.ICheckUserBiz;
import com.memorypalace.user.biz.IRegisterUserBiz;
import com.memorypalace.util.AJaxUtil;

@Controller
@RequestMapping("/")
@SessionAttributes("user")
public class UserAction {
	
	@Autowired
	private IRegisterUserBiz registerUserBiz;
	@Autowired
	private ICheckUserBiz checkUserBiz;  
	@Autowired
	private INoteTypeBiz noteTypeBiz;
	
	
	@RequestMapping("useraction_registerUser")
	public String registerUser(User user){
		user.setUserPoints(100);
		user.setUserPower(1);
		// 新建用户对象
		registerUserBiz.registerUser(user);
		// 配置默认的笔记类型
		noteTypeBiz.addDefaultNoteType(user.getUserId());
		// 配置默认的卡片样式
		
		return "/login.jsp";
	}
	
	
	//验证用户名是否存在
	@RequestMapping("useraction_checkUserName")
	public Boolean checkPhone(HttpServletResponse response,User user){
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Boolean flag= registerUserBiz.getUserByUserName(user);
		if (flag) {
			out.print("1");
		} else {
			out.print("0");
		}

		out.flush();
		out.close();
		return null;
		
	}
	
	
	
	//验证邮箱是否已经存在
	@RequestMapping("useraction_checkUserEmail")
	public Boolean checkEmail(HttpServletResponse response,User user){
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Boolean flag= registerUserBiz.getUserByUserEmail(user);
		if (flag) {
			out.print("1");
		} else {
			out.print("0");
		}

		out.flush();
		out.close();
		return null;
		
	}
	
	
	
	//登录
	@RequestMapping("checkuser_userandpsd")
	public String checkUser(User user,Map<String,Object> map,HttpServletRequest request){
			User userGetFromDB = checkUserBiz.checkUserAndPsd(user);
			if(userGetFromDB!=null){
				map.put("user", userGetFromDB);
				return "redirect:index-login.html";
			}else{
				String msg="用户名或密码错误";
				map.put("msg",msg);
				return "/login.jsp";
			}
	
	}
	
	@RequestMapping("getuser")
	@ResponseBody
	public void getuser(HttpServletRequest request,HttpServletResponse response){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null){
			user.getUserId();
		}else{
			AJaxUtil.sendJsonByObj(user, response);
		}
	}
	
	
	@RequestMapping("loginout")
	public String loginout(HttpServletRequest request){
		System.out.println(request.getSession().getAttribute("user")+"???????????????");
		request.getSession().removeAttribute("user");
		System.out.println(request.getSession().getAttribute("user")+"================");
		return "/login.jsp";
	}
	

	
}
